<template>
  <!-- 辅助检查 -->
  <section>
    <div style="text-align:right;">
      <el-button @click="dialogSelectAssistTemplate = true">选择模板</el-button>
    </div>
    <br />
    <table width="100%" border="0">
      <tr v-for="(item, index) in form" :key="index">
        <td>
          <dir class="item-box">
            <div class="form-tool">
              <div>
                <el-tooltip :disabled="!item.price" effect="dark" :content="'￥' + item.price" placement="top">
                  <el-link :underline="false" type="primary">{{ item.checkName }}，部位：{{ item.positionId | changeAssist(checkPartArr) }}</el-link>
                </el-tooltip>
              </div>
              <div>
                <el-link v-if="!toDisabled(item)" :underline="false" class="vertical-middle" @click.prevent="onDelItem(item, index)">
                  {{item.isCombo}}
                  <i class="el-icon-close"></i>
                </el-link>
              </div>
            </div>
          </dir>
        </td>
        <td width="100" style="text-align:center;line-height:36px;vertical-align: top;">
          <!-- <el-input disabled :value="item.checkupNum+item.unit"></el-input> -->
          <!-- {{item.unit|changeAssist(unitEnum)}} -->
          {{ item.assistNum }}次
        </td>
        <td width="300" style="vertical-align: top;">
          <el-input v-model="item.remark" :disabled="item.payStatus != 2" placeholder="备注"></el-input>
        </td>
      </tr>
    </table>

    <br />
    <div>
      <el-select
        v-model="curItem"
        value-key="id"
        filterable
        remote
        clearable
        @clear="getCheckAssistPageList"
        :remote-method="getCheckAssistPageList"
        @change="onChangeCheckItem"
        placeholder="请选择辅助检查"
        style="width:100%"
      >
        <el-option v-for="(item, index) in checkItemArr" :key="index" :label="item.checkName" :value="item"></el-option>
        <br />
        <pager v-model="pager" @change-pagenum="getPage"></pager>
      </el-select>
    </div>
    <br />
    <div class="form-tool">
      <div>
        <el-button type="primary" :loading="btnLoading" @click="onSubmit">保存</el-button>
      </div>
      <div>
        <el-button v-if="form.length > 0 && !templateSave.t4" @click="dialogPrint = true">
          <i class="iconfont icondayin vertical-middle" style="font-size:20px;"></i>
          <span class="vertical-middle">打印申请单</span>
        </el-button>
      </div>
    </div>
    <dialogSelectAssistItem v-model="dialogSelectAssistItem" :itemData="curItem" @change="onSelectPart"></dialogSelectAssistItem>
    <dialogSelectAssistTemplate v-model="dialogSelectAssistTemplate" @change="onChangeTemplate"></dialogSelectAssistTemplate>
    <print v-model="dialogPrint" :list="form" :patientId="patientId" :cureId="cureId" :checkPart="checkPartArr"></print>
  </section>
</template>
<script>
export default {
  components: {
    dialogSelectAssistItem: () => import("./_com/dialogSelectAssistItem.vue"),
    dialogSelectAssistTemplate: () => import("./_com/dialogSelectAssistTemplate.vue"),
    print: () => import("../_print/assistCheck.vue")
  },
  computed: {
    templateSave() {
      return this.$store.state.doctorMZ.templateSave;
    },
    templateData() {
      return this.$store.state.doctorMZ.templateData;
    },
    unitEnum() {
      return this.$store.state.doctorMZ.enum.unitEnum;
    },
    checkPartArr() {
      return this.$store.state.doctorMZ.enum.checkPartEnum;
    },
    patientId() {
      return this.$store.state.doctorMZ.patientId;
    },
    doctorId() {
      return this.$store.state.doctorMZ.doctorId;
    },
    deptId() {
      return this.$store.state.doctorMZ.deptId;
    },
    cureId() {
      return this.$store.state.doctorMZ.cureId;
    }
  },
  data() {
    return {
      pager: { pageSize: 10, pageNum: 1, total: 0 },
      searchForm: {
        status: 1,
        checkName: ""
      },
      dialogPrint: false,
      isOpenEdit: false,
      cureMedicalRelateId: "",
      btnLoading: false,
      dialogSelectAssistItem: false,
      dialogSelectAssistTemplate: false,
      form: [],
      curItem: "",
      checkItemArr: []
    };
  },
  created() {
  },
  mounted() {
    this.getCheckAssistPageList();
    this.getData();
  },
  methods: {
    onSubmit() {
      let th = this;
      if (!th.cureMedicalRelateId && th.form.length == 0) return;
      let update = () => {
        let j = {
          cureMedicalRelateId: th.cureMedicalRelateId,
          cureAssist: JSON.stringify(th.form)
        };
        th.btnLoading = true;
        th.$api
          .updateCureAssist(j)
          .then(res => {
            if (res.executed) {
              th.$message.success("保存完成");
              th.getData("reset");
            } else {
              th.$message.error(res.message);
            }
            th.btnLoading = false;
          })
          .catch(() => {
            th.btnLoading = false;
          });
      };
      let add = () => {
        let j = {
          cureMedicalRelateId: th.cureMedicalRelateId,
          cureAssist: JSON.stringify(th.form)
        };
        th.btnLoading = true;
        th.$api
          .addMultipleCureAssist(j)
          .then(res => {
            if (res.executed) {
              th.$message.success("保存完成");
              th.getData("reset");
            } else {
              th.$message.error(res.message);
            }
            th.btnLoading = false;
          })
          .catch(() => {
            th.btnLoading = false;
          });
      };
      th.isOpenEdit = false;
      if (th.cureMedicalRelateId) update();
      else add();
    },
    //选择模板
    onChangeTemplate(obj) {
      let th = this;
      // let arr = [];
      obj.baseTemplateAdviceBeans.forEach(item => {
        let display = [];
        if (typeof item.display == "string") display = JSON.parse(item.display);
        display.forEach(d => {
          let j = {
            cureId: th.cureId,
            hpId: th.adminUser.hpId,
            patientId: th.patientId,
            deptId: th.deptId,
            doctorId: th.doctorId,
            assistId: item.adviceId,
            price: item.retailPrice ? item.retailPrice : 0,
            positionId: d,
            sellMoney: "",
            unit: item.unit,
            id: "",
            assistNum: item.count ? d.count : 1,
            checkName: item.adviceDesc,
            remark: item.remark
          };
          th.addItem(j);
          // arr.push(j);
        });
      });
      // th.form.push(...arr);
    },
    //选择部位
    onSelectPart(arr) {
      let th = this;
      console.log(arr);
      arr.forEach(item => {
        let j = {
          cureId: th.cureId,
          hpId: th.adminUser.hpId,
          patientId: th.patientId,
          deptId: th.deptId,
          doctorId: th.doctorId,
          assistId: th.curItem.id,
          positionId: item,
          unit: th.curItem.unit,
          price: th.curItem.price,
          sellMoney: th.curItem.sellMoney,
          assistNum: 1,
          id: "",
          checkName: th.curItem.checkName,
          remark: ""
        };
        // th.form.push(j);
        th.addItem(j);
      });
    },
    //删除项
    onDelItem(obj, index) {
      let th = this;
      if (obj.id) {
        let j = {
          cureAssistId: obj.id
        };
        th.$api.cureAssistpreDelete(j).then(res => {
          if (res.executed && res.data) {
            th.form.splice(index, 1);
          } else {
            th.$message.warning(res.message);
          }
        });
      }else if(a){

      } else {
        th.form.splice(index, 1);
      }
    },
    getData(reset) {
      let th = this;
      let j = {
        cureId: th.cureId
      };
      th.$api.getCureAssistCureId(j).then(res => {
        if (reset) {
          th.form = [];
        }
        const list=res.data;
        list.forEach((data, index) => {
          if (index == 0) th.cureMedicalRelateId = data.cureMedicalRelateId;
          let j = {
            cureId: th.cureId,
            hpId: th.adminUser.hpId,
            patientId: th.patientId,
            deptId: th.deptId,
            doctorId: th.doctorId,
            assistId: data.cureAssistInfo.assistId,
            positionId: data.positionId,
            unit: data.unit,
            status: data.cureAssistInfo.status,
            payStatus: data.payStatus,
            price: data.price ? Number.parseFloat(data.price) : 0,
            assistNum: data.cureAssistInfo.assistNum,
            checkName: data.name,
            id: data.cureAssistInfo.id,
            remark: data.cureAssistInfo.remark,
          };
          th.addItem(j);
        });
        th.$nextTick(function() {
          th.isOpenEdit = true;
          if (reset) {
            th.$store.commit("Set_templateSave", {
              key: "t4",
              value: false
            });
          }
        });
      });
    },
    //选择项目
    onChangeCheckItem(obj) {
      let th = this;
      if (!obj) return;
      th.dialogSelectAssistItem = true;
    },
    getPage() {
      let th = this;
      let j = {
        hpId: th.adminUser.hpId
      };
      Object.assign(j, th.pager, th.searchForm);
      th.$api.getCheckAssistPageList(j).then(res => {
        if (res.executed) {
          th.checkItemArr = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
      });
    },
    getCheckAssistPageList(checkName) {
      let th = this;
      th.searchForm.checkName = checkName;
      th.pager.pageNum = 1;
      th.getPage();
    },
    //添加项目
    addItem(json) {
      const th = this;
      if (typeof json.payStatus == "undefined") {
        json.payStatus = 2;
      }
      if (typeof json.status == "undefined") {
        json.status = 0;
      }
      if (json.payStatus == 2 && json.status == 0) {
        // let data = th.form.find(d => {
        //   return d.assistId == json.assistId && d.positionId == json.positionId && json.payStatus == 2 && json.status == 0;
        // });
        // console.log(data);
        // if (data) {
        //   return;
        // }
        th.form.push(json);
      } else {
        th.form.push(json);
      }
    },
    //清空未付款的数据
    delListData() {
      const th = this;
      let newArr = th.form.filter(d => {
        return d.payStatus != 2 || d.status != 0;
      });
      th.form = newArr;
    },
    //判断是否可编辑
    toDisabled(obj) {
      if (obj.payStatus == 2 && obj.status == 0) {
        return false;
      } else {
        return true;
      }
    }
  },
  watch: {
    form: {
      deep: true,
      handler: function(n, o) {
        let th = this;
        if (!th.isOpenEdit) return;
        let bo = true;
        if (n.length == 0 && !th.cureMedicalRelateId) {
          bo = false;
        }
        //开启修改状态
        th.$store.commit("Set_templateSave", {
          key: "t4",
          value: bo
        });
      }
    },
    //监听大模板数据
    templateData: {
      deep: true,
      immediate: true,
      handler(n, o) {
        const th = this;
        let data = n.t4;
        //判断是否有值
        if (!data) {
          return;
        }
        //清空数据
        if (n.isEmpty) {
          th.delListData();
        }
        data.forEach(ele => {
          //添加数据
          let j = {
            cureId: th.cureId,
            hpId: th.adminUser.hpId,
            patientId: th.patientId,
            deptId: th.deptId,
            doctorId: th.doctorId,
            assistId: ele.id,
            positionId: Number.parseInt(ele.position),
            unit: ele.unit,
            price: ele.sellMoney,
            assistNum: 1,
            checkName: ele.checkName,
            id: "",
            remark: ele.remark
          };
          j.sellMoney = ele.sellMoney * j.assistNum;
          th.addItem(j);
        });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.item-box {
  background-color: #f3f3f3;
  margin: 0;
  padding: 9px 10px;
  border-radius: 7px;
  cursor: pointer;
}
</style>
